﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

using NUnit.Framework;

using Microsoft.Practices.Unity;

using ERPStore.Extensions;

namespace ERPStore.NewsLetters.Tests.Controllers
{
	[TestFixture]
	public class NewsLettersControllerTests : ERPStore.Tests.TestBase
	{
		public NewsLettersControllerTests()
		{

		}

		[SetUp]
		public override void Initialize()
		{
			base.Initialize();

			var container = ERPStore.GlobalConfiguration.Configuration.DependencyResolver.GetContainer();
			container.RegisterType<CloudSoft.Repositories.IRepository<NewsLetters.Repositories.NewsLettersDbContext>, CloudSoft.Repositories.MemoryRepository<NewsLetters.Repositories.NewsLettersDbContext>>();

			this.CryptoService = container.Resolve<ERPStore.Services.CryptoService>();
		}

		protected ERPStore.Services.CryptoService CryptoService { get; private set; }

		[Test]
		public void NewsLetter_Without_Key()
		{
			var controller = CreateController<ERPStore.Web.NewsLetters.Controllers.NewsLettersController>();

			var result = controller.NewsLetter(null) as RedirectToRouteResult;

			Assert.AreEqual(result.RouteName, ERPStore.Web.Routing.ERPStoreRoutes.HOME);
		}

		[Test]
		public void NewsLetter_Null_Tag()
		{
			var controller = CreateController<ERPStore.Web.NewsLetters.Controllers.NewsLettersController>();

			var result = controller.Tag(null) as EmptyResult;

			Assert.IsNotNull(result);
		}

		[Test]
		public void NewsLetter_Bad_Tag()
		{
			var controller = CreateController<ERPStore.Web.NewsLetters.Controllers.NewsLettersController>();

			var result = controller.Tag(Guid.NewGuid().ToString()) as EmptyResult;

			Assert.IsNotNull(result);
		}

		[Test]
		public void NewsLetter_Tag()
		{
			var trakingKey = new ERPStore.NewsLetters.Models.TrackingKey()
			{
				Email = string.Format("{0}@erpstore.net", Guid.NewGuid()),
				NewsLetterId = 1,
				CampaignId = 1,
				CreationDate = DateTime.Now,
				LinkName = "open",
				EmailRecipientId = 0,
			};

			var encryptedKey = CryptoService.Encrypt(trakingKey);

			var controller = CreateController<ERPStore.Web.NewsLetters.Controllers.NewsLettersController>();
			var result = controller.Tag(encryptedKey) as FileStreamResult;

			Assert.IsNotNull(result);

			System.Threading.Thread.Sleep(1 * 1000);
		}
	}
}
